﻿using Jinndev.UI;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using UnityEngine.Networking;
using System.Collections;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Jinndev.Json;

public class OtherUtil
{



    public static void OnClickGenerateTileExcel()
    {
        string imageDir = @"F:\Work\JingDong\Compose\trunk\program\client\Compose\assets\bundle\atlas";
        if (!Directory.Exists(imageDir))
        {
            NotificationUI.Create("目标不存在");
            return;
        }

        StringBuilder listBuilder = new StringBuilder();
        StringBuilder slotBuilder = new StringBuilder();

        string[] dirs = Directory.GetDirectories(imageDir);
        foreach (string dir in dirs)
        {
            string dirName = Path.GetFileName(dir);
            if (!dirName.StartsWith("room"))
            {
                continue;
            }
            if (!int.TryParse(dirName.Replace("room", ""), out int roomId))
            {
                NotificationUI.Create("房间编号错误: " + dirName);
                continue;
            }

            string[] files = Directory.GetFiles(dir, "*.png", SearchOption.AllDirectories);
            int skip = 0;
            string lastSlot = null;

            foreach (var file in files)
            {
                string name = Path.GetFileNameWithoutExtension(file);
                if (name == "_")
                {
                    skip++;
                    continue;
                }
                string slot = GetSlotName(name);

                // 列表
                string group = "G-" + slot;
                string num = name.EndsWith("D") ? "1" : "0";

                listBuilder.Append(name).Append("\t").Append(group).Append("\t").Append("\t").Append(num).Append("\n");

                // 槽位
                if (lastSlot == slot)
                {
                    continue;
                }
                lastSlot = slot;

                slotBuilder.Append(roomId).Append("\t").Append(slot).Append("\t").Append("G-" + slot).Append("\n");
            }
            Debug.Log(dirName + " 图片数: " + (files.Length - skip));
        }

        string outputList = @"F:\Work\JingDong\Compose\trunk\design\家具列表.txt";
        File.WriteAllText(outputList, listBuilder.ToString());
        NotificationUI.Create(outputList);

        string slotOutput = @"F:\Work\JingDong\Compose\trunk\design\家具槽.txt";
        File.WriteAllText(slotOutput, slotBuilder.ToString());
        NotificationUI.Create(slotOutput);
    }

    //public static void GetFurnitureSlot() {
    //    string imageDir = @"F:\Work\JingDong\Compose\trunk\program\client\Compose\assets\bundle\atlas";
    //    if (!Directory.Exists(imageDir)) {
    //        NotificationUI.Create("目标不存在");
    //        return;
    //    }

    //    StringBuilder builder = new StringBuilder();
    //    string[] files = Directory.GetFiles(imageDir, "*.png", SearchOption.AllDirectories);

    //    string lastSlot = null;

    //    foreach (var file in files) {
    //        string name = Path.GetFileNameWithoutExtension(file);
    //        if (name == "_" || name.StartsWith("毛坯")) {
    //            continue;
    //        }
    //        string slot = GetSlotName(name);

    //        if (lastSlot == slot) {
    //            continue;
    //        }
    //        lastSlot = slot;

    //        builder.Append("1").Append("\t").Append(slot).Append("\t").Append("G-" + slot).Append("\n");
    //    }
    //    string output = @"F:\Work\JingDong\Compose\trunk\design\家具槽.txt";
    //    File.WriteAllText(output, builder.ToString());
    //    NotificationUI.Create(output);
    //}

    private static string GetSlotName(string name)
    {
        if (name.EndsWith("A") || name.EndsWith("B") || name.EndsWith("C") || name.EndsWith("D"))
        {
            name = name.Substring(0, name.Length - 1);
        }
        if (name.EndsWith("_"))
        {
            name = name.Substring(0, name.Length - 1);
        }
        return name;
    }


}
